package com.douguo.recipe;

import com.douguo.recipe.view.NavigationView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class EKArticleActivity extends DouguoActivity {
	
	private WebView mWebView;
	private ProgressBar mProgressBar;
	private NavigationView mNavigationView;
	private TextView mTextView;
	
	private String mUrl;
	
	private String mSortTitle;
	private String mTitle;
	
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ek_article);
		initData();
		initBundle();
		initUI();
	}
	
	private void initBundle() {
		Intent intent = getIntent();
		if(intent != null) {
			Bundle bundle = intent.getExtras();
			mSortTitle = bundle.getString("title");
			mTitle = bundle.getString("article_title");
		}
	}
	
	private void initData() {
		Intent intent = getIntent();
		Bundle extra = intent.getExtras();
		mUrl = extra.getString("article_url");
	}
	
	private void initUI() {
		mTextView = (TextView) findViewById(R.id.ek_article_title);
		mTextView.setText(mTitle);
		mTextView.setVisibility(View.GONE);
		mNavigationView = new NavigationView(this, NavigationView.STATE_MAGAZINE);
		mProgressBar = (ProgressBar) findViewById(R.id.ek_article_progress);
		mWebView = (WebView) findViewById(R.id.ek_article_webview);
		mWebView.loadUrl(mUrl);
		mWebView.setWebChromeClient(new WebChromeClient(){
			public void onProgressChanged(WebView view, int progress) {
                //Activity和Webview根据加载程度决定进度条的进度大小
               //当加载到100%的时候 进度条自动消失
				mProgressBar.setProgress(progress);
				if(progress == 100) {
					mProgressBar.postDelayed(new Runnable() {
						
						@Override
						public void run() {
							mProgressBar.setVisibility(View.GONE);
							
						}
					}, 1000);
				}
			}
		});
		findViewById(R.id.imageButton_back).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
				
			}
		});
		if(mSortTitle != null) {
			setTitle(mSortTitle);
		}
		else {
			setTitle(getString(R.string.titleMeiShiEKan));
		}
	}
	
}
